package com.tianxiang.action;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.annotations.JSON;

import com.opensymphony.xwork2.ActionSupport;
import com.tianxiang.model.User;
import com.tianxiang.service.ManageFileService;
import com.tianxiang.utils.Tools;

public class ManageFileAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6137422174463801450L;

	private String srcName; // 要操作的源文件或文件夹
	private String delPath;//文件夹位置
	private String target; // 要复制或移动到的目的位置
	private String msg; // 返回客户端的数据
	private boolean success; // 返回客户端成功与否的标志
	private float usedspace; //剩余使用空间


	private static ManageFileService service = new ManageFileService();

	public float getUsedspace() {
		return usedspace;
	}
	
	@JSON(serialize = false)
	public String getSrcName() {
		return srcName;
	}

	public void setSrcName(String srcName) {
		this.srcName = srcName;
	}
	
	@JSON(serialize = false)
	public String getDelPath() {
		return delPath;
	}

	public void setDelPath(String delPath) {
		this.delPath = delPath;
	}

	@JSON(serialize = false)
	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	/**
	 * 复制文件的Action
	 * 
	 * @return
	 */
	public String copy() {
		User user=(User)ServletActionContext.getRequest().getSession()
				.getAttribute("loginuser");
		String username=user.getUsername();
		int id=user.getId();
		String realSrcPath = srcName;
		String fileName=Tools.getFileNameByPath(realSrcPath);
		String realTargetPath = Tools.getAcessPath(getUserRoot(),"", target)+"\\"+fileName;
		boolean result = service.copy(username,fileName, realTargetPath,id);
		usedspace=user.getUsedspace();
		if (result) {
			msg = "成功将\"" + srcName + "\"拷贝到" + target;
			success = true;
		} else {
			msg = "操作失，请稍后重试！";
			success = false;
		}
		return SUCCESS;
	}

	/**
	 * 移动文件的Action
	 * 
	 * @return
	 */
	public String move() {
		User user=(User)ServletActionContext.getRequest().getSession()
				.getAttribute("loginuser");
		String username=user.getUsername();
//		String realSrcPath = Tools.getAcessPath(getUserRoot(),"", srcName);
		String realSrcPath = srcName;
		String fileName=Tools.getFileNameByPath(realSrcPath);
		System.out.println("getUserRoot():"+getUserRoot());
		String realTargetPath = Tools.getAcessPath(getUserRoot(),"", target)+"\\"+fileName;
		System.out.println("target="+target);
		System.out.println("把文件从 "+realSrcPath+" 移动到 "+realTargetPath);
		 
		boolean result = service.move(username,fileName, realTargetPath);
		if (result) {
			msg = "成功将\"" + srcName + "\"移动到" + target;
			success = true;
		} else {
			msg = "操作失，请稍后重试！";
			success = false;
		} 
		
		return SUCCESS;
	}

	/**
	 * 删除文件的Action
	 * 
	 * @return
	 * @throws ParseException 
	 */
	public String delete() throws ParseException {
		User user=(User)ServletActionContext.getRequest().getSession()
				.getAttribute("loginuser");
		int userId=user.getId();
		String userName = user.getUsername();
		String realPath = delPath;
		String fileName = Tools.getFileNameByPath(realPath);
		String recycalPath = Tools.getRecycalPath(userName);
		String filename = srcName;
		File file =new File(realPath);
		boolean result;
		if(file.isDirectory()){
			result = service.delFolder(filename,realPath,recycalPath);
		}else{
			result = service.delToRecycal(userId,userName,fileName);
		}
		usedspace=user.getUsedspace();
		if (result) {
			success = true;
			msg = "删除" + srcName + "成功！";
		} else {
			success = false;
			msg = "操作失败！";
		}
		return SUCCESS;
	}
	/**
	 * 永久删除文件的Action
	 * 
	 * @return
	 */
	public String deletePer() {
		User user=(User)ServletActionContext.getRequest().getSession()
				.getAttribute("loginuser");
		String userName = user.getUsername();
		String fileName = srcName;
		String userPath = delPath;
		File f = new File(delPath);
		boolean result;
		if(f.isDirectory()){
			result = service.delFolderPermanent(userPath);
		}else{
			
			result = service.delPermanent(userName,fileName);
		}
		if (result) {
			success = true;
			msg = "删除" + srcName + "成功！";
		} else {
			success = false;
			msg = "操作失败！";
		}
		return SUCCESS;
	}
	/**
	 * 文件还原Action
	 * 
	 * @return
	 */
	public String reverseFile() {
		System.out.println("reverseFile");
		User user=(User)ServletActionContext.getRequest().getSession()
				.getAttribute("loginuser");
		int userId=user.getId();
		String userName=user.getUsername();
		String fileName=srcName;
		String path=delPath;
		File f = new File(path);
		String userPath=Tools.getUserPath(userName);
		boolean result;
		if(f.isDirectory()){
			result = service.reverseFolder(userId,fileName,userPath,path);
		}else{
			result = service.reverse(userId,userName,fileName);
		}
		usedspace=user.getUsedspace();
		if (result) {
			success = true;
			msg = "删除" + srcName + "成功！";
		} else {
			success = false;
			msg = "操作失败！";
		}
		return SUCCESS;
	}
	
	public String createFolder(){
		String realPath = Tools.getAcessPath(getUserRoot(),"", target);
		System.out.println("'创建文件夹："+realPath);
		try {
			boolean result=service.newFile(realPath, true);
			if(result){
				success=true;
			}else {
				success=false;
			}
		} catch (IOException e) {
			e.printStackTrace();
			success=false;
		}
		return SUCCESS;
	} 
	
	String getUserRoot(){
		User user=(User)ServletActionContext.getRequest().getSession()
				.getAttribute("loginuser");
String userroot=user.getUsername();
return userroot;
	}
}
